package palms.messages;

import java.io.IOException;
import java.net.Socket;

import javax.crypto.SealedObject;

import palms.exceptions.InvalidClassMessage;
import palms.exceptions.InvalidTicketMessage;
import palms.exceptions.InvalidUserMessage;
import palms.security.PalmsSecurityManager;

public class TGSResponse extends PMessage {

	
	private static final long serialVersionUID = 2715194883693771101L;
	
	private SealedObject ticketSI;
	private SealedObject ticketSL;
	private SealedObject keyChain;

	public TGSResponse(SealedObject ticketSI, SealedObject ticketSL, SealedObject keyChain) {
		this.ticketSI = ticketSI;
		this.ticketSL = ticketSL;
		this.keyChain = keyChain;
	}
	public TGSResponse() {
	}
	public SealedObject getTicketSI() {
		return ticketSI;
	}
	public void setTicketSI(SealedObject ticketSI) {
		this.ticketSI = ticketSI;
	}
	public SealedObject getTicketSL() {
		return ticketSL;
	}
	public void setTicketSL(SealedObject ticketSL) {
		this.ticketSL = ticketSL;
	}
	public SealedObject getKeyChain() {
		return keyChain;
	}
	public void setKeyChain(SealedObject keyChain) {
		this.keyChain = keyChain;
	}

	public void sendTroughSocket(Socket s) throws IOException{
		PalmsSecurityManager.sendObjectTroughtSocket(s, new TGSResponse());
		PalmsSecurityManager.sendObjectTroughtSocket(s, this.getTicketSI());
		PalmsSecurityManager.sendObjectTroughtSocket(s, this.getTicketSL());
		PalmsSecurityManager.sendObjectTroughtSocket(s, this.getKeyChain());
	}
	
	public static TGSResponse receiveTroughSocket(Socket s) throws IOException, ClassNotFoundException, InvalidClassMessage, InvalidUserMessage, InvalidTicketMessage{
		Object o = PalmsSecurityManager.receiveObjectTroughSocket(s);
		if(!(o instanceof TGSResponse))
			PalmsSecurityManager.throwCommException(o);
		else{
			TGSResponse tr = (TGSResponse)o;
			SealedObject siticket = (SealedObject) PalmsSecurityManager.receiveObjectTroughSocket(s);
			SealedObject slticket = (SealedObject) PalmsSecurityManager.receiveObjectTroughSocket(s);
			SealedObject kchain = (SealedObject) PalmsSecurityManager.receiveObjectTroughSocket(s);
			tr.setTicketSI(siticket);
			tr.setTicketSL(slticket);
			tr.setKeyChain(kchain);
			return tr;
		}
		return null;
	}

}
